yjx.controller('clientDetailController', ['$rootScope', '$scope', '$timeout', '$stateParams', '$fetch', 'modalService', '$state', 'wxShareService', '$init', function ($rootScope, $scope, $timeout, $stateParams, $fetch, modalService, $state, wxShareService, $init) {
    //
    $scope.tabList = [{ id: 1, name: '关系树' }, { id: 2, name: '推荐文章' }, { id: 3, name: '健康服务' }, { id: 4, name: '产品计划书' }, { id: 5, name: '购买过' }, { id: 6, name: '检测报告' }]
    $scope.curTab = $scope.tabList[0];
    $scope.isEdit = true;
    $scope.parent = {};/**与子页面共享数据 */
    $init.title("客户详情")
    var tabApi = '/customer/navigation/data';
    var curSwiper;
    var tabList;
    var container = document.getElementById('clientSwiper');
    var clientDetail = angular.element(document.getElementById('clientDetail'));
    var width = document.body.offsetWidth;
    var HEIGHT = $('body').height();
    $scope.scrollLeft = 80;
    $scope.miniheight={
        'min-height':HEIGHT - 215
    }
    $timeout(function () {
        //curSwipe  r.controller.control = tabList;//curSwiper控制tabList，需要在tabList初始化后
        buildSwiper();
        // setAutoHeight();
    }, 1000)
    /**获取客户详情 */
    $scope.customerId = $stateParams.id;
    var api = '/customer/detail';
    function getClientDetail() {
        $fetch.fetch(
            api,
            {
                params: {
                    customerId: $scope.customerId
                },
                cache: false
            }
        ).then(function (data) {
            $scope.editClient = data;
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }
    if ($scope.customerId) {
        getClientDetail();
    }
    /**编辑客户信息 */
    $scope.editInfo = function () {
        $scope.showInfo = true;
        $state.go('clientDetail.editInfo');
    }
    /**swiper */
    function buildSwiper() {
        tabList = new Swiper('#tabList2', {
            slidesPerView: 3,
            slideToClickedSlide: true,
        });
        curSwiper = new Swiper('#clientSwiper', {
            direction: 'horizontal',
            //noSwiping : true,
            slider: width,
            on: {
                slideNextTransitionStart: function () {
                    tabList.slideTo(curSwiper.activeIndex)
                    $timeout(function () {
                        $scope.curTab = $scope.tabList[curSwiper.activeIndex];
                    })
                },
                slidePrevTransitionStart: function () {
                    tabList.slideTo(curSwiper.activeIndex - 1)
                    $timeout(function () {
                        $scope.curTab = $scope.tabList[curSwiper.activeIndex];
                    })
                },
                slideChangeTransitionEnd: function () {
                    getDatas(curSwiper.activeIndex);
                }
            }
        })
    }
    $scope.tabSelect = function (item, index) {
        $scope.curTab = item;
        curSwiper.slideTo(index, 500);
    }
    /**swiper自适应内容高度 */
    function setAutoHeight() {
        $timeout(function () {
            // angular.element(container).height(height);
            var height = angular.element(container.querySelector('.swiper-slide-active')).find('>div').height();
            // console.log(height); || curSwiper.activeIndex == 3
            if (curSwiper.activeIndex == 0 || curSwiper.activeIndex == 3) {
                height = height + 30;
                clientDetail.removeClass('h100');
            } else {
                clientDetail.addClass('h100');
            }
            // if (height == 0) {
            //     height = 200;
            // }
            angular.element(container).height(height);
            // console.log( angular.element(container).height());
        })
    }
    $scope.resetHeight = function () {
        $timeout(function () {
            setAutoHeight();
        })
    }
    /**获取各项数据 */
    function getDatas(index) {
        switch (index) {
            case 0:
                break;
            // case 1://推荐话术
            // if(!$scope.callList){
            //     getCallList();
            // };
            // $scope.resetHeight();
            // break;
            case 1://推荐文章
                if (!$scope.resourceList) {
                    getResourceList();
                };
                $scope.resetHeight();
                break;
            case 2://健康服务
                if (!$scope.serviceList) {
                    getServiceList();
                    getCallList();
                };
                $scope.resetHeight();
                break;
            case 3://产品计划书
                if (!$scope.parent.plan) {
                    getProductPlan();
                };
                if (!$scope.parent.producRectList) {
                    getProducRectList();
                }
                $scope.resetHeight();
                break;
            case 4://购买过
                if (!$scope.productList) {
                    getProductList();
                };
                $scope.resetHeight();
                break;
            case 5://检测报告
                if (!$scope.reportList) {
                    getReportList();
                };
                $scope.resetHeight();
                break;
        }
    }
    /**关系树 共享数据*/
    $scope.share = {
        relationList: [],
        clientList: []
    }
    /**推荐话术 */
    function getCallList() {
        $fetch.fetch(
            tabApi,
            {
                params: {
                    userId: $rootScope.userId,
                    customerId: $scope.customerId,
                    dataType: 's'
                },
                cache: false
            }
        ).then(function (data) {
            $timeout(function () {
                $scope.callList = data;
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }
    /**推荐文章 */
    function getResourceList() {
        $fetch.fetch(
            tabApi,
            {
                params: {
                    userId: $rootScope.userId,
                    customerId: $scope.customerId,
                    dataType: 'a'
                },
                cache: false
            }
        ).then(function (data) {
            $timeout(function () {
                $scope.resourceList = data;
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }
    /**健康服务 */
    function getServiceList() {
        $fetch.fetch(
            tabApi,
            {
                params: {
                    userId: $rootScope.userId,
                    customerId: $scope.customerId,
                    dataType: 'w'
                },
                cache: false
            }
        ).then(function (data) {
            $timeout(function () {
                $scope.serviceList = data;
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }
    /**产品计划书 */
    function getProductPlan() {
        $fetch.fetch(
            tabApi,
            {
                params: {
                    userId: $rootScope.userId,
                    customerId: $scope.customerId,
                    dataType: 'j'
                },
                cache: false
            }
        ).then(function (data) {
            $timeout(function () {
                $scope.parent.plan = data;
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }
    wxShareService.onHideMenuItems();
    $scope.forward = function () {
        var productIdStr = '';
        for (var i = 0; i < $scope.parent.producRectList.length; i++) {
            if (!productIdStr) {
                productIdStr = $scope.parent.producRectList[i].id
            } else {
                productIdStr = productIdStr + ',' + $scope.parent.producRectList[i].id
            }
        };
        $scope.link = APPSRC + '#/' + "productPlan?productIds=" + productIdStr + "&forwardId=" + $rootScope.userId + "&customerId=" + $scope.customerId + "&discountPrice=" + $scope.parent.discountPrice;

        wxShareService.onMenuShareAppMessage('', '产品计划书', '', $scope.link, $scope.callback)
        // console.log(APPSRC + '#/' + "productPlan?productIds=" + productIdStr + "&forwardId=" + $rootScope.userId + "&customerId=" + $scope.customerId + "&discountPrice=" + $scope.parent.discountPrice)
        modalService.mainMsgInfo("点击右上角将产品计划书转发给客户");
    }
    $scope.callback = function () {
    }

    /**编辑产品计划书 */
    $scope.editPlan = function () {
        $scope.editPlanTag = true;
        // clientDetail.addClass('h100');
        $state.go('clientDetail.editPlan');
    }

    /**推荐产品 */
    function getProducRectList() {
        $fetch.fetch(
            tabApi,
            {
                params: {
                    userId: $rootScope.userId,
                    customerId: $scope.customerId,
                    dataType: 'r'
                },
                cache: false
            }
        ).then(function (data) {
            $timeout(function () {
                $scope.parent.producRectList = data;
                $scope.parent.totalPrice=0; 
                $scope.parent.discountPrice = 0;
                if ($scope.parent.producRectList && $scope.parent.producRectList.length > 0) {
                    for (var i = 0; i < $scope.parent.producRectList.length; i++) {
                        var price = $scope.parent.producRectList[i].price ? $scope.parent.producRectList[i].price : $scope.parent.producRectList[i].originalPrice;
                        $scope.parent.totalPrice = $scope.parent.totalPrice + price;
                    }
                }
                $scope.resetHeight();
            }, 200)
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }
    /**已购买 */
    function getProductList() {
        $fetch.fetch(
            tabApi,
            {
                params: {
                    userId: $rootScope.userId,
                    customerId: $scope.customerId,
                    dataType: 'b'
                },
                cache: false
            }
        ).then(function (data) {
            $timeout(function () {
                $scope.productList = data;
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }
    /**检查报告 */
    function getReportList() {
        $fetch.fetch(
            tabApi,
            {
                params: {
                    userId: $rootScope.userId,
                    customerId: $scope.customerId,
                    dataType: 'c'
                },
                cache: false
            }
        ).then(function (data) {
            $timeout(function () {
                $scope.reportList = data;
            })
        }).catch(function (e) {
            modalService.mainMsgError(e.msg);
        })
    }
    $scope.pdfView = function (item) {
        $scope.pdfUrl = item.pdfUrl;
        $state.go('clientDetail.pdfDetail')
    }

    // $scope.productPlan = '<p><img src="http://genetuan.oss-cn-shenzhen.aliyuncs.com/images/1512993657309036005.jpg" title="1512993657309036005.jpg"/></p>';
}])
